package com.ptu.service.impl;


import com.ptu.entity.Auditing;
import com.ptu.mapper.AuditingMapper;
import com.ptu.mapper.ExpenseMapper;
import com.ptu.service.AuditingService;
import com.ptu.util.DBUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AuditingServiceImpl  implements AuditingService {

    @Override
    public int addAudit(Auditing auditing, Integer money, String mgrid) {
        SqlSession sqlSession = DBUtil.getSqlSession();
        AuditingMapper mapper = sqlSession.getMapper(AuditingMapper.class);
        ExpenseMapper mapper1 = sqlSession.getMapper(ExpenseMapper.class);

        // 当前报销单状态
        String result = auditing.getResult();

        if("2".equals(result)) {
            // 证明报销审核通过

            if(mgrid == null) {
                // 证明当前同意打款的用户的CEO
                    // 修改报销单状态
                    mapper1.update(auditing.getExpId(), null, result);
                    // 同意打款
            } else {
                // 判断当前的报销金额
                if(money>2000){
                    //修改报销单的下一个审核人
                    mapper1.update(auditing.getExpId(),mgrid,"");
                }else{
                    //修改报销单的状态
                    mapper1.update(auditing.getExpId(),null,result);
                    //通知财务进行打款
                }
            }
        } else {
            // 证明报销单拒绝或者打回

            // 修改报销单状态
            mapper1.update(auditing.getExpId(), null, result);

        }

        //增加报销单记录信息
        int insert = mapper.insert(auditing);

        DBUtil.closeAll();

        return insert;
    }

    @Override
    public List<Auditing> findAuditing(int expId) {
        AuditingMapper mapper = DBUtil.getSqlSession().getMapper(AuditingMapper.class);

        List<Auditing> auditings = mapper.selectAudit(expId);

        DBUtil.closeAll();
        return auditings;
    }

    @Override
    public List<Auditing> findMyAudit(String empId) {
        AuditingMapper mapper = DBUtil.getSqlSession().getMapper(AuditingMapper.class);

        List<Auditing> auditExpense = mapper.selectMyAudit(empId);

        DBUtil.closeAll();
        return auditExpense;
    }
}
